<template>
  <div>
    <div class="gva-search-box">
      <el-form ref="elSearchFormRef" :inline="true" :model="searchInfo" class="demo-form-inline" :rules="searchRule"
               @keyup.enter="onSubmit">
        <el-form-item label="卡号" prop="cardId">
          <el-input v-model="searchInfo.cardId" placeholder="搜索条件"/>
        </el-form-item>
        <el-form-item label="开卡人" prop="name">
          <el-input v-model="searchInfo.name" placeholder="搜索条件"/>
        </el-form-item>
        <el-form-item label="身份证" prop="idNumber">
          <el-input v-model="searchInfo.idNumber" placeholder="搜索条件"/>
        </el-form-item>
        <el-form-item label="乡镇" prop="townCode">
          <el-select v-model="searchInfo.townCode" clearable placeholder="请选择"
                     @clear="()=>{searchInfo.townCode=undefined}">
            <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="searchInfo.state" clearable placeholder="请选择" @clear="()=>{searchInfo.state=undefined}">
            <el-option v-for="(item,key) in card_stateOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="惩戒" prop="smite">
          <el-select v-model="searchInfo.smite" clearable placeholder="请选择" @clear="()=>{searchInfo.smite=undefined}">
            <el-option v-for="(item,key) in global_boolOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="下发时间" prop="issueAt">
          <template #label>
            <span>
              下发时间
              <el-tooltip content="搜索范围是开始日期（包含）至结束日期（不包含）">
                <el-icon><QuestionFilled/></el-icon>
              </el-tooltip>
            </span>
          </template>
          <el-date-picker v-model="searchInfo.startIssueAt" type="datetime" placeholder="开始日期"
                          :disabled-date="time=> searchInfo.endIssueAt ? time.getTime() > searchInfo.endIssueAt.getTime() : false"></el-date-picker>
          —
          <el-date-picker v-model="searchInfo.endIssueAt" type="datetime" placeholder="结束日期"
                          :disabled-date="time=> searchInfo.startIssueAt ? time.getTime() < searchInfo.startIssueAt.getTime() : false"></el-date-picker>
        </el-form-item>

        <template v-if="showAllQuery">
          <el-form-item label="类型" prop="type">
            <el-select v-model="searchInfo.type" clearable placeholder="请选择" @clear="()=>{searchInfo.type=undefined}">
              <el-option v-for="(item,key) in card_typeOptions" :key="key" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="开户银行/营业商" prop="openIn">
            <el-select v-model="searchInfo.openIn" clearable placeholder="请选择"
                       @clear="()=>{searchInfo.openIn=undefined}">
              <el-option v-for="(item,key) in bank_nameOptions" :key="key" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="开卡时间" prop="openAt">
            <template #label>
            <span>
              开卡时间
              <el-tooltip content="搜索范围是开始日期（包含）至结束日期（不包含）">
                <el-icon><QuestionFilled/></el-icon>
              </el-tooltip>
            </span>
            </template>
            <el-date-picker v-model="searchInfo.startOpenAt" type="datetime" placeholder="开始日期"
                            :disabled-date="time=> searchInfo.endOpenAt ? time.getTime() > searchInfo.endOpenAt.getTime() : false"></el-date-picker>
            —
            <el-date-picker v-model="searchInfo.endOpenAt" type="datetime" placeholder="结束日期"
                            :disabled-date="time=> searchInfo.startOpenAt ? time.getTime() < searchInfo.startOpenAt.getTime() : false"></el-date-picker>
          </el-form-item>
          <!-- 将需要控制显示状态的查询条件添加到此范围内 -->
        </template>

        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
          <el-button link type="primary" icon="arrow-down" @click="showAllQuery=true" v-if="!showAllQuery">展开
          </el-button>
          <el-button link type="primary" icon="arrow-up" @click="showAllQuery=false" v-else>收起</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <div class="gva-btn-list">
        <el-button v-auth="btnAuth.add" type="primary" icon="plus" @click="openDialog()">新增</el-button>
        <el-button v-auth="btnAuth.batchDelete" icon="delete" style="margin-left: 10px;"
                   :disabled="!multipleSelection.length" @click="onDelete">删除
        </el-button>
      </div>
      <el-table
          ref="multipleTable"
          style="width: 100%"
          tooltip-effect="dark"
          :data="tableData"
          row-key="ID"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="30"/>
        <el-table-column align="left" label="下发时间" prop="issueAt" width="100">
          <template #default="scope">{{ formatDate(scope.row.issueAt) }}</template>
        </el-table-column>
        <el-table-column align="left" label="线索编号" prop="no" width="120" :show-overflow-tooltip="true"/>
        <el-table-column align="left" label="开卡人" prop="name" width="80" :show-overflow-tooltip="true"/>
        <el-table-column align="left" label="卡号" prop="cardId" width="120" :show-overflow-tooltip="true"/>
        <el-table-column align="left" label="类型" prop="type" width="80">
          <template #default="scope">
            {{ filterDict(scope.row.type, card_typeOptions) }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="身份证" prop="idNumber" width="120" :show-overflow-tooltip="true"/>
        <el-table-column align="left" label="乡镇" prop="townCode" width="60">
          <template #default="scope">
            {{ filterDict(scope.row.townCode, townOptions) }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="联系电话" prop="phone" width="120"/>
        <el-table-column align="left" label="开卡时间" prop="openAt" width="100">
          <template #default="scope">{{ formatDate(scope.row.openAt) }}</template>
        </el-table-column>
        <el-table-column align="left" label="银行/营业商" prop="openIn" width="120" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ filterDict(scope.row.openIn, bank_nameOptions) }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="开户网点" prop="openAddress" width="120" :show-overflow-tooltip="true"/>
        <el-table-column align="left" label="主办人" prop="fOwnerId" width="80">
          <template #default="scope">
            <span>{{ filterDataSource(dataSource.fOwnerId, scope.row.fOwnerId) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="left" label="案件编号" prop="caseNo" width="120" :show-overflow-tooltip="true"/>
        <el-table-column align="left" label="状态" prop="state" width="80">
          <template #default="scope">
            {{ filterDict(scope.row.state, card_stateOptions) }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="惩戒" prop="smite" width="80">
          <template #default="scope">
            {{ filterDict(scope.row.smite, global_boolOptions) }}
          </template>
        </el-table-column>
        <el-table-column align="left" label="截止时间" prop="exceedAt" width="180">
          <template #default="scope">{{ formatDate(scope.row.exceedAt) }}</template>
        </el-table-column>
        <el-table-column align="left" label="操作" fixed="right" :min-width="appStore.operateMinWith">
          <template #default="scope">
            <el-button v-auth="btnAuth.info" type="primary" link class="table-button" @click="getDetails(scope.row)">
              <el-icon style="margin-right: 5px">
                <InfoFilled/>
              </el-icon>
              查看
            </el-button>
            <el-button v-auth="btnAuth.edit" type="primary" link icon="edit" class="table-button"
                       @click="updateCasCardsFunc(scope.row)">编辑
            </el-button>
            <el-button v-auth="btnAuth.delete" type="primary" link icon="delete" @click="deleteRow(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-drawer destroy-on-close :size="appStore.drawerSize" v-model="dialogFormVisible" :show-close="false"
               :before-close="closeDialog" :close-on-click-modal="false">
      <template #header>
        <div class="flex justify-between items-center">
          <span class="text-lg">{{ type === 'create' ? '新增' : '编辑' }}</span>
          <div>
            <el-button :loading="btnLoading" type="primary" @click="enterDialog">确 定</el-button>
            <el-button @click="closeDialog">取 消</el-button>
          </div>
        </div>
      </template>

      <el-form :model="formData" label-position="top" ref="elFormRef" :rules="rule" label-width="80px">
        <el-form-item label="线索编号:" prop="no">
          <el-input v-model="formData.no" :clearable="true" placeholder="请输入线索编号"/>
        </el-form-item>
        <el-form-item label="案件编号:" prop="caseNo">
          <el-input v-model="formData.caseNo" :clearable="true" placeholder="请输入案件编号"/>
        </el-form-item>
        <el-form-item label="类型:" prop="type">
          <el-select v-model="formData.type" placeholder="请选择类型" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in card_typeOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="卡号:" prop="cardId">
          <el-input v-model="formData.cardId" :clearable="true" placeholder="请输入卡号"/>
        </el-form-item>
        <el-form-item label="开卡人:" prop="name">
          <el-input v-model="formData.name" :clearable="true" placeholder="请输入开卡人"/>
        </el-form-item>
        <el-form-item label="身份证:" prop="idNumber">
          <el-input v-model="formData.idNumber" :clearable="true" placeholder="请输入身份证"/>
        </el-form-item>
        <el-form-item label="联系电话:" prop="phone">
          <el-input v-model="formData.phone" :clearable="true" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="开卡时间:" prop="openAt">
          <el-date-picker v-model="formData.openAt" type="date" style="width:100%" placeholder="选择日期"
                          :clearable="true"/>
        </el-form-item>
        <el-form-item label="开户银行/营业商:" prop="openIn">
          <el-select v-model="formData.openIn" placeholder="请选择开户银行/营业商" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in bank_nameOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="开户网点:" prop="openAddress">
          <el-input v-model="formData.openAddress" :clearable="true" placeholder="请输入开户网点"/>
        </el-form-item>
        <el-form-item label="乡镇:" prop="townCode">
          <el-select v-model="formData.townCode" placeholder="请选择乡镇" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="居住地址:" prop="address">
          <el-input v-model="formData.address" :clearable="true" placeholder="请输入居住地址"/>
        </el-form-item>
        <el-form-item label="原因:" prop="reason">
          <el-input v-model="formData.reason" :clearable="true" placeholder="请输入原因"/>
        </el-form-item>
        <el-form-item label="主办人:" prop="fOwnerId">
          <el-select v-model="formData.fOwnerId" placeholder="请选择主办人" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in dataSource.fOwnerId" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="状态:" prop="state">
          <el-select v-model="formData.state" placeholder="请选择状态" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in card_stateOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="惩戒:" prop="smite">
          <el-select v-model="formData.smite" placeholder="请选择惩戒" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in global_boolOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="附件:" prop="attachments">
          <SelectImage
              multiple
              v-model="formData.attachments"
              file-type="image"
          />
        </el-form-item>
        <el-form-item label="下发时间:" prop="issueAt">
          <el-date-picker v-model="formData.issueAt" type="date" style="width:100%" placeholder="选择日期"
                          :clearable="true"/>
        </el-form-item>
        <el-form-item label="截止时间:" prop="exceedAt">
          <el-date-picker v-model="formData.exceedAt" type="date" style="width:100%" placeholder="选择日期"
                          :clearable="true"/>
        </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer destroy-on-close :size="appStore.drawerSize" v-model="detailShow" :show-close="true"
               :before-close="closeDetailShow" title="查看">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="线索编号">
          {{ detailFrom.no }}
        </el-descriptions-item>
        <el-descriptions-item label="案件编号">
          {{ detailFrom.caseNo }}
        </el-descriptions-item>
        <el-descriptions-item label="类型">

          {{ filterDict(detailFrom.type, card_typeOptions) }}

        </el-descriptions-item>
        <el-descriptions-item label="卡号">
          {{ detailFrom.cardId }}
        </el-descriptions-item>
        <el-descriptions-item label="开卡人">
          {{ detailFrom.name }}
        </el-descriptions-item>
        <el-descriptions-item label="身份证">
          {{ detailFrom.idNumber }}
        </el-descriptions-item>
        <el-descriptions-item label="联系电话">
          {{ detailFrom.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="开卡时间">
          {{ detailFrom.openAt }}
        </el-descriptions-item>
        <el-descriptions-item label="开户银行/营业商">
          {{ filterDict(detailFrom.openIn, bank_nameOptions) }}
        </el-descriptions-item>
        <el-descriptions-item label="开户网点">
          {{ detailFrom.openAddress }}
        </el-descriptions-item>
        <el-descriptions-item label="乡镇">
          {{ filterDict(detailFrom.townCode, townOptions) }}
        </el-descriptions-item>
        <el-descriptions-item label="居住地址">
          {{ detailFrom.address }}
        </el-descriptions-item>
        <el-descriptions-item label="原因">
          {{ detailFrom.reason }}
        </el-descriptions-item>
        <el-descriptions-item label="主办人">
          <span>{{ filterDataSource(dataSource.fOwnerId, detailFrom.fOwnerId) }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="状态">

          {{ filterDict(detailFrom.state, card_stateOptions) }}

        </el-descriptions-item>
        <el-descriptions-item label="惩戒">

          {{ filterDict(detailFrom.smite, global_boolOptions) }}

        </el-descriptions-item>
        <el-descriptions-item label="附件">
          <el-image style="width: 50px; height: 50px; margin-right: 10px"
                    :preview-src-list="returnArrImg(detailFrom.attachments)" :initial-index="index"
                    v-for="(item,index) in detailFrom.attachments" :key="index" :src="getUrl(item)" fit="cover"/>
        </el-descriptions-item>
        <el-descriptions-item label="下发时间">
          {{ detailFrom.issueAt }}
        </el-descriptions-item>
        <el-descriptions-item label="截止时间">
          {{ detailFrom.exceedAt }}
        </el-descriptions-item>
      </el-descriptions>
    </el-drawer>

  </div>
</template>

<script setup>
import {
  createCasCards,
  deleteCasCards,
  deleteCasCardsByIds,
  findCasCards,
  getCasCardsDataSource,
  getCasCardsList,
  updateCasCards
} from '@/api/card/casCards'
import {getUrl} from '@/utils/image'
// 图片选择组件
import SelectImage from '@/components/selectImage/selectImage.vue'

// 全量引入格式化工具 请按需保留
import {filterDataSource, filterDict, formatDate, getDictFunc, getNewYearStart, returnArrImg} from '@/utils/format'
import {ElMessage, ElMessageBox} from 'element-plus'
import {reactive, ref} from 'vue'
// 引入按钮权限标识
import {useBtnAuth} from '@/utils/btnAuth'
import {useAppStore} from "@/pinia"


defineOptions({
  name: 'CasCards'
})
// 按钮权限实例化
const btnAuth = useBtnAuth()

// 提交按钮loading
const btnLoading = ref(false)
const appStore = useAppStore()

// 控制更多查询条件显示/隐藏状态
const showAllQuery = ref(false)

// 自动化生成的字典（可能为空）以及字段
const townOptions = ref([])
const global_boolOptions = ref([])
const bank_nameOptions = ref([])
const card_typeOptions = ref([])
const card_stateOptions = ref([])
const formData = ref({
  no: '',
  caseNo: '',
  type: '',
  cardId: '',
  name: '',
  idNumber: '',
  phone: '',
  openAt: new Date(),
  openIn: '',
  openAddress: '',
  townCode: '',
  reason: '',
  fOwnerId: undefined,
  state: '',
  smite: '',
  attachments: [],
  issueAt: new Date(),
  exceedAt: new Date(),
  address: '',
})
const dataSource = ref([])
const getDataSourceFunc = async () => {
  const res = await getCasCardsDataSource()
  if (res.code === 0) {
    dataSource.value = res.data
  }
}
getDataSourceFunc()


// 验证规则
const rule = reactive({
  type: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  cardId: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  name: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  idNumber: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  openIn: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  townCode: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  issueAt: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
  ],
  exceedAt: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
  ],
})

const searchRule = reactive({
  openAt: [{
    validator: (rule, value, callback) => {
      if (searchInfo.value.startOpenAt && searchInfo.value.endOpenAt && (searchInfo.value.startOpenAt.getTime() === searchInfo.value.endOpenAt.getTime() || searchInfo.value.startOpenAt.getTime() > searchInfo.value.endOpenAt.getTime())) {
        callback(new Error('开始日期应当早于结束日期'))
      } else {
        callback()
      }
    }, trigger: 'change'
  }],
  issueAt: [{
    validator: (rule, value, callback) => {
      if (searchInfo.value.startIssueAt && searchInfo.value.endIssueAt && (searchInfo.value.startIssueAt.getTime() === searchInfo.value.endIssueAt.getTime() || searchInfo.value.startIssueAt.getTime() > searchInfo.value.endIssueAt.getTime())) {
        callback(new Error('开始日期应当早于结束日期'))
      } else {
        callback()
      }
    }, trigger: 'change'
  }],
})

const elFormRef = ref()
const elSearchFormRef = ref()

// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({
  startIssueAt: getNewYearStart(),
  sort: 'issue_at',
  order: 'desc'
})
// 重置
const onReset = () => {
  searchInfo.value = {
    sort: 'issue_at',
    order: 'desc'
  }
  getTableData()
}

// 搜索
const onSubmit = () => {
  elSearchFormRef.value?.validate(async (valid) => {
    if (!valid) return
    page.value = 1
    getTableData()
  })
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async () => {
  const table = await getCasCardsList({page: page.value, pageSize: pageSize.value, ...searchInfo.value})
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () => {
  townOptions.value = await getDictFunc('town')
  global_boolOptions.value = await getDictFunc('global_bool')
  bank_nameOptions.value = await getDictFunc('bank_name')
  card_typeOptions.value = await getDictFunc('card_type')
  card_stateOptions.value = await getDictFunc('card_state')
}

// 获取需要的字典 可能为空 按需保留
setOptions()


// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 删除行
const deleteRow = (row) => {
  ElMessageBox.confirm('确定要删除吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteCasCardsFunc(row)
  })
}

// 多选删除
const onDelete = async () => {
  ElMessageBox.confirm('确定要删除吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const IDs = []
    if (multipleSelection.value.length === 0) {
      ElMessage({
        type: 'warning',
        message: '请选择要删除的数据'
      })
      return
    }
    multipleSelection.value &&
    multipleSelection.value.map(item => {
      IDs.push(item.ID)
    })
    const res = await deleteCasCardsByIds({IDs})
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      if (tableData.value.length === IDs.length && page.value > 1) {
        page.value--
      }
      getTableData()
    }
  })
}

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateCasCardsFunc = async (row) => {
  const res = await findCasCards({ID: row.ID})
  type.value = 'update'
  if (res.code === 0) {
    formData.value = res.data
    dialogFormVisible.value = true
  }
}


// 删除行
const deleteCasCardsFunc = async (row) => {
  const res = await deleteCasCards({ID: row.ID})
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    if (tableData.value.length === 1 && page.value > 1) {
      page.value--
    }
    getTableData()
  }
}

// 弹窗控制标记
const dialogFormVisible = ref(false)

// 打开弹窗
const openDialog = () => {
  type.value = 'create'
  dialogFormVisible.value = true
}

// 关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false
  formData.value = {
    no: '',
    caseNo: '',
    type: '',
    cardId: '',
    name: '',
    idNumber: '',
    phone: '',
    openAt: new Date(),
    openIn: '',
    openAddress: '',
    townCode: '',
    reason: '',
    fOwnerId: undefined,
    state: '',
    smite: '',
    attachments: [],
    issueAt: new Date(),
    exceedAt: new Date(),
  }
}
// 弹窗确定
const enterDialog = async () => {
  btnLoading.value = true
  elFormRef.value?.validate(async (valid) => {
    if (!valid) return btnLoading.value = false
    let res
    switch (type.value) {
      case 'create':
        res = await createCasCards(formData.value)
        break
      case 'update':
        res = await updateCasCards(formData.value)
        break
      default:
        res = await createCasCards(formData.value)
        break
    }
    btnLoading.value = false
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '创建/更改成功'
      })
      closeDialog()
      getTableData()
    }
  })
}


const detailFrom = ref({})

// 查看详情控制标记
const detailShow = ref(false)


// 打开详情弹窗
const openDetailShow = () => {
  detailShow.value = true
}


// 打开详情
const getDetails = async (row) => {
  // 打开弹窗
  const res = await findCasCards({ID: row.ID})
  if (res.code === 0) {
    detailFrom.value = res.data
    openDetailShow()
  }
}


// 关闭详情弹窗
const closeDetailShow = () => {
  detailShow.value = false
  detailFrom.value = {}
}


</script>

<style>

</style>
